<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/11 16:51
 * Blog：www.myblogs.xyz
 */

namespace app\admin\controller;


use app\common\service\CommonService;
use think\facade\View;

class Menu extends Common
{
    public function index()
    {
        $menu_model = new \app\common\model\Menu();
        $list = $menu_model->getList();
        View::assign('list', $list);
        return View::fetch();
    }

    public function insertMenu()
    {
        $this->saveMenu();
        return View::fetch('save_menu');
    }

    public function updateMenu()
    {
        $this->saveMenu();
        return View::fetch('save_menu');
    }

    protected function saveMenu()
    {
        $menu_model = new \app\common\model\Menu();
        if (request()->isPost())
        {
            $data = input('post.');
            if (!$menu_model->operation($data)){
                $this->returnError($menu_model->getError());
            }
            $this->returnSuccess('保存成功');
        }
        $menu_id = input('param.menu_id', 0, 'intval');
        $parent_id = input('param.parent_id', 0, 'intval');
        $is_show = input('param.is_show', 0, 'intval');
        $top_list = $menu_model->getList(['parent_id'=>0]);
        $oldData = [];
        if (!empty($menu_id)){
            $oldData = $menu_model->getFind($menu_id);
            $parent_id = $oldData['parent_id'];
        }

        View::assign([
            'parent_id'=>$parent_id,
            'top_list'=>$top_list,
            'is_show'=>$is_show,
            'oldData'=>$oldData
        ]);
    }

    public function methodMenu()
    {
        View::config(['layout_on'=>false]);
        $parent_id = input('param.parent_id',0,'intval');
        $menu_model = new \app\common\model\Menu();
        if (request()->isPost())
        {
            $data = input('post.');
            if (!empty($data['new'])){
                // true Batch write
                $mew_list = [];
                foreach ($data['new'] as $key=>$val){
                    $val['parent_id'] = $parent_id;
                    $mew_list[] = $val;
                }
                if (!$menu_model->operation($mew_list, true, true)){
                    $this->returnError($menu_model->getError());
                }
            }
            if (!empty($data['old'])){
                // Batch update on data
                $old_list = [];
                foreach ($data['old'] as $key=>$val){
                    $val['menu_id'] = $key;
                    $old_list[] = $val;
                }
                if (!$menu_model->operation($old_list, true, true)){
                    $this->returnError($menu_model->getError());
                }
            }
            $this->returnSuccess('保存成功', url('Menu/index'));
        }
        $list = $menu_model->getList(['parent_id'=>$parent_id]);
        View::assign([
            'parent_id'=>$parent_id,
            'list'=>$list
        ]);
        return View::fetch();
    }

    public function auditMenu()
    {
        if (CommonService::audit('is_show')){
            $this->jsonReturn(1,'','操作成功');
        }
        $this->jsonReturn(0,'','操作失败');
    }

    // Update the sorting
    public function updateSort()
    {
        if (request()->isPost())
        {
            $sort = input('post.', false);
            if (!$sort){
                $this->returnError('更新失败');
            }
            $menu_model = new \app\common\model\Menu();
            foreach ($sort['sort'] as $key=>$val){
                $data = [
                    'menu_id'=>$key,
                    'sort'=>$val
                ];
                $menu_model->update($data);
            }
            $this->returnSuccess('更新成功');
        }
    }

    public function deleteMenu()
    {
        if (CommonService::delete()){
            $this->returnError('删除失败');
        }
        $this->returnSuccess('删除成功');
    }

}